HW 03

Author

Nandakumar Kuthalaraja

if (!require("pacman")) 
  install.packages("pacman")

pacman::p_load(tidyverse,gghighlight,openintro,lubridate,palmerpenguins,glue,scales,countdown,ggthemes,gt,openintro,ggrepel,patchwork,quantreg,janitor,colorspace,broom,fs,here,dsbox,ggridges,gtable,ggimage,ggpubr,cowplot,png,sysfonts,showtext,grid)

# set theme for ggplot2
ggplot2::theme_set(ggplot2::theme_minimal(base_size = 14))

#Add Fonts as Needed
font_add("oldFont", "font/Wolfsmith.ttf")  
showtext_auto()

# set width of code output
options(width = 85)

# set figure parameters for knitr
knitr::opts_chunk$set(
  fig.width = 7,        # 7" width
  fig.asp = 0.618,      # the golden ratio
  fig.retina = 3,       # dpi multiplier for displaying HTML output on retina
  fig.align = "center", # center align figures
  dpi = 300             # higher dpi, sharper image
)

1 - Du Bois challenge.

# Read Data 
income <- read_csv("data/income.csv")

# Recode final class label for line break
income <- income |>
  mutate(
    Class = recode(Class, "$1000 AND OVER" = "1000\nAND OVER")
  )

# left table
label_table <- income |>
  mutate(
    Class = factor(Class, levels = rev(c(
      "$100-200", "$200-300", "$300-400", "$400-500",
      "$500-750", "$750-1000", "1000\nAND OVER"
    ))),
    row = as.numeric(Class)
  ) |>
  ggplot(aes(y = Class)) +
  geom_tile(aes(x = 1.5, width = 2, height = 1), fill = NA, color = "black") +
  geom_segment(
    data = tibble(y = seq(0.5, 7.5, 1)),
    aes(x = 0.9, xend = 2.9, y = y, yend = y),
    inherit.aes = FALSE,
    color = "gray7", linewidth = 0.2
  ) +
  geom_segment(
    data = tibble(x = c(0.9, 1.95, 2.9)),
    aes(x = x, xend = x, y = 0.5, yend = 7.5),
    inherit.aes = FALSE,
    color = "gray7", linewidth = 0.2
  ) +
  geom_text(aes(x = 1, label = Class), hjust = 0, size = 3.2, family = "oldFont") +
  geom_text(aes(x = 2, label = paste0("$", Average_Income)), hjust = 0, size = 3.2, family = "oldFont") +
  scale_y_discrete(limits = rev(c(
    "$100-200", "$200-300", "$300-400", "$400-500",
    "$500-750", "$750-1000", "1000\nAND OVER"
  ))) +
  xlim(0.9, 2.9) +
  labs(title = NULL, x = NULL, y = NULL) +
  theme_void() +
  theme(
    plot.margin = margin(5, 0, 5, 5),
    axis.text.y = element_blank(),
    plot.background = element_rect(fill = NA, color = NA),  
    panel.background = element_rect(fill = NA, color = NA)
  )

data <- income |>
  pivot_longer(cols = Rent:Other, names_to = "type", values_to = "measurement") |>
  mutate(
    type = factor(type, levels = c("Rent", "Food", "Clothes", "Tax", "Other")),
    Class = factor(Class, levels = rev(c(
      "$100-200", "$200-300", "$300-400", "$400-500",
      "$500-750", "$750-1000", "1000\nAND OVER"
    )))
  ) 
data$measurement[data$measurement == 0] <- 0.1

category <- c(
  Rent = "#000000",
  Food = "#8663A3",
  Clothes = "#E89A8D",
  Tax = "slategray3",
  Other = "#D6C8B0"
)

bar_plot <- ggplot(data, aes(x = Class, y = measurement, fill = type)) +
  geom_bar(stat = "identity", width = 0.8) +
  geom_text(
    aes(label = ifelse(measurement >= 1, paste0(measurement, "%"), "")),
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "white"
  ) +
  scale_y_reverse() +
  scale_fill_manual(values = category) +
  coord_flip() +
  labs(
    title = "", 
  x = NULL,
  y = NULL,
  caption = "FOR FURTHER STATISTICS RAISE THIS FRAME."
  ) +
 
  theme(
    axis.text.y = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.major.x = element_blank(),
    legend.title = element_blank(),
    legend.position = "top",
    legend.direction = "horizontal",
    legend.justification = "center",
     plot.caption = element_text(
       family = "oldFont",
      hjust = 0.5,       
      size = 7,
      margin = margin(t = 10)
    ),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    panel.grid = element_blank()
  
  )


combined_plot <- (label_table + bar_plot + plot_layout(widths = c(1.3, 3)))

final_plot <- ggbackground(combined_plot, "images/parchment_paper-1074131_1920.png")
print(final_plot)

2 - COVID survey - interpret

3 - COVID survey - reconstruct

4 - COVID survey - re-reconstruct

5 - COVID survey - another view